//
//  DetailViewController.m
//  HuateVideo
//
//  Created by Tiger on 12-5-20.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "DetailViewController.h"
#import "Utilities.h"
#import <QuartzCore/QuartzCore.h>
#import "Movie.h"
#import "CommonWebServices.h"
#import "DataList.h"
#import"SeriesSelectingViewController.h"
@interface DetailViewController ()

@end

@implementation DetailViewController
@synthesize channelType,movie,type;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [table release];
    [array release];
    [headerView release];
    [infoLabel release];
    [mayLabel release];
    [jiantou release];
    [downArray release];
    [requestArray release];
    [super dealloc];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    self.view.backgroundColor = TOOLBAR_TINTCOLOR;
    self.title = LS(@"详情");
    
    array = [[NSMutableArray alloc]init];
    downArray = [[NSMutableArray alloc]init]; 
    requestArray = [[NSMutableArray alloc]init]; 
    showDetail = NO;
    
    if (type == 1) {
        if ([[movie Type] intValue] == MovieChannelTypeHot) {
            channelType = MovieChannelTypeMovie;
        }
        else if ([[movie Type] intValue] == MovieChannelTypeMovie) {
            channelType = MovieChannelTypeMovie;
        }
        else if ([[movie Type] intValue] == MovieChannelTypeSeries) {
            channelType = MovieChannelTypeSeries;
        }
        else if ([[movie Type] intValue] == MovieChannelTypeMicroViedo) {
            channelType = MovieChannelTypeMicroViedo;
        }
        else if ([[movie Type] intValue] == MovieChannelTypeMusic) {
            channelType = MovieChannelTypeMusic;
        }
        else if ([[movie Type] intValue] == MovieChannelTypePic) {
            channelType = MovieChannelTypePic;
        }
        else if ([[movie Type] intValue] == MovieChannelTypeNews) {
            channelType = MovieChannelTypeNews;
        }
        else if ([[movie Type] intValue] == MovieChannelTypeOriginal) {
            channelType = MovieChannelTypeOriginal;
        }
    }
    
    table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 367)];
    table.dataSource = self;
    table.delegate = self;
    table.separatorStyle = UITableViewCellSeparatorStyleNone;
    table.backgroundColor = [UIColor clearColor];
    [self.view addSubview:table];
    
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle: LS(@"播放")
                                                                      style:UIBarButtonItemStylePlain
                                                                     target: self 
                                                                     action: @selector(toPlayVideo)];
    self.navigationItem.rightBarButtonItem = barButtonItem;
        
    [self.view showProgressText:YES];
    if (type == 1) {
        [self loadDetail];
    }
    else {
        //[self initHeaderView];
        [self loadDetail];
    }
}


- (void)loadDetail
{
    NSString *action = @"";
    NSString *class = @"";
    
    if (channelType == MovieChannelTypeMovie) {
        action = @"GetDetailMovie";
        class = @"Movie";
    }
    else if (channelType == MovieChannelTypeSeries) {
        action = @"GetDetailDelePlay";
        class = @"DelePlay";
    }
    else if (channelType == MovieChannelTypeMicroViedo) {
        action = @"GetDetailVideo";
        class = @"Video";
    }
    else if (channelType == MovieChannelTypeMusic) {
        action = @"GetDetailMusic";
        class = @"Music";
    }
    else if (channelType == MovieChannelTypePic) {
        action = @"GetDetailPicture";
        class = @"Picture";
    }
    else if (channelType == MovieChannelTypeNews) {
        action = @"GetDetailMessage";
        class = @"MessageList";
    }
    else if (channelType == MovieChannelTypeOriginal) {
        action = @"GetDetailOriginal";
        class = @"Original";
    }
    
    NSString *rootXML = [NSString stringWithFormat:@"%@Result",action];
    
    CommonWebServices *webServ = [[CommonWebServices alloc] init];
    webServ.tag = 101;
    NSMutableDictionary *parma = [NSMutableDictionary dictionary];
    [parma setValue:[movie News_id] forKey:@"id"];
    webServ.delegate = self;
    [requestArray addObject:webServ];
    [webServ startRequestWithMode:@"Default" Action:action Parma:parma Class:class RootXML:rootXML resultIsArray:NO];
    [webServ release];
}

#pragma mark - CommonWebServicesDelegate

- (void)onCommonWebServicesWithCustomClass:(id)object tag:(int)tag {
    if (object) {
        if (tag == 101) {
            self.movie = object;
            [self initHeaderView];
        }
        else if (tag == 102) {
            NSArray *movieList = ((DataList *)object).List;
            if ([movieList count]>0) {
                [array addObjectsFromArray:movieList];
                [table reloadData];
            }
            
            [self.view showProgressText:NO];
        }
        else if (tag == 103) {
            UIButton *playBtn = (UIButton *)[headerView viewWithTag:1001];
            playBtn.enabled = YES;
            self.navigationItem.rightBarButtonItem.enabled = YES;
        }
    }
    else {
        [self.view showProgressText:NO];
    }
}

- (void)onCommonWebServicesFaild
{
    [self.view showProgressText:NO];
    
    UIAlertView	*alertview = [[UIAlertView alloc] initWithTitle:LS(@"加载失败")  
                                                        message:LS(@"请检查网络连接和设置")
                                                       delegate:self 	
                                              cancelButtonTitle:LS(@"确定")
                                              otherButtonTitles:nil];		
    [alertview show];		
    [alertview release];
}

- (void)playOne:(UIButton *)sender
{
    for (UIButton *b in v.subviews) {
        b.backgroundColor = [UIColor clearColor];
    }
    
    sender.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
    
    if ([[movie Type] intValue] == MovieChannelTypeSeries) {
        
        NSMutableArray *tmp = [NSMutableArray array];
        for (int i=0; i<[[movie File_path_array] count]; i++) {
            if ([[[movie File_path_array] objectAtIndex:i] isKindOfClass:[NSString class]]) {
                if ([[[movie File_path_array] objectAtIndex:i] length]>5) {
                    [tmp addObject:[[movie File_path_array] objectAtIndex:i]];
                }
            }
        }
        [movie setFile_path_array:tmp];
        
        [movie setOrderNUM:sender.tag];
    }
    
    [[NSNotificationCenter defaultCenter] postNotificationName:PLAYMOVIENOTIFICATION object:movie];
}

- (void)reflashIndex:(NSInteger)index
{
    NSInteger base = 24*index;
    
    for (UIView* subView in [v subviews])
	{
		[subView removeFromSuperview];
	}
    
    NSInteger total = [movie CountNum];
    
    for (int i=1+base; i<=total && i<=24+base; i++) {
        NSString *index = [NSString stringWithFormat:@"%d",i];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(37.5*((i-base-1)%8), 23*floor((CGFloat)(i-base-1)/8.0), 37.5, 23);
        btn.tag = i-1;
        [btn addTarget:self action:@selector(playOne:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitle:index forState:UIControlStateNormal];
        [btn setTitleColor:LIGHT_COLOR forState:UIControlStateHighlighted];
        btn.titleLabel.font = [UIFont systemFontOfSize:14.0];
        btn.backgroundColor = [UIColor clearColor];
        [v addSubview:btn];
    }
}

- (void)setDelePlayIndex:(UIButton *)sender
{
    [self reflashIndex:sender.tag- 1000];
}

- (void)initHeaderView
{
    headerView = [[UIView alloc]init];
    
    CGFloat y = 0;
    if (channelType == MovieChannelTypeSeries) {
        y = 100+7;
        
        v = [[UIView alloc]initWithFrame:CGRectMake(10, 157, 300, 69)];
        v.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
        v.layer.masksToBounds = YES; 
        v.layer.cornerRadius = 6.0; 
        [headerView addSubview:v];
        
        NSInteger total = [movie CountNum];
        
        for (int i=1; i<=total && i<=24; i++) {
            NSString *index = [NSString stringWithFormat:@"%d",i];
            
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(37.5*((i-1)%8), 23*floor((CGFloat)(i-1)/8.0), 37.5, 23);
            btn.tag = i-1;
            [btn addTarget:self action:@selector(playOne:) forControlEvents:UIControlEventTouchUpInside];
            [btn setTitle:index forState:UIControlStateNormal];
            [btn setTitleColor:LIGHT_COLOR forState:UIControlStateHighlighted];
            btn.titleLabel.font = [UIFont systemFontOfSize:14.0];
            btn.backgroundColor = [UIColor clearColor];
            [v addSubview:btn];
        }
        
        for (int i=0; i<ceil((CGFloat)total/24.0); i++) {
            NSString *str = [NSString stringWithFormat:@"%d-%d",i*24+1,(i+1)*24];
            
            if ((i+1)*24>total) {
                str = [NSString stringWithFormat:@"%d-%d",i*24+1,total];
                
                if (i*24+1 == total) {
                    str = [NSString stringWithFormat:@"%d",total];
                }
            }
            
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(10+75*i, 227, 75, 23);
            btn.tag = 1000+i;
            [btn addTarget:self action:@selector(setDelePlayIndex:) forControlEvents:UIControlEventTouchUpInside];
            [btn setTitle:str forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:14.0];
            btn.backgroundColor = [UIColor clearColor];
            [headerView addSubview:btn];
        }
    }
    else {
        y = 7;
    }
    
    headerView.frame = CGRectMake(0, 0, 320, 250+y);
    headerView.backgroundColor = [UIColor clearColor];
    table.tableHeaderView = headerView;
    
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 75, 95)];
    image.tag = 100;
    image.image = [UIImage imageNamed:@"dengdai.png"];
    [headerView addSubview:image];
    [image release];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(95, 10, 200, 20)];
    label.text = [movie News_title];
    label.textColor = LIGHT_COLOR;
    label.font = [UIFont systemFontOfSize:16.0];
    label.backgroundColor = [UIColor clearColor];
    [headerView addSubview:label];
    [label release];
    
    label = [[UILabel alloc]initWithFrame:CGRectMake(95, 35, 200, 20)];
    label.text = [NSString stringWithFormat:@"%@: %@",LS(@"导演"),[movie News_arg3]];
    label.textColor = [UIColor lightGrayColor];
    label.font = [UIFont systemFontOfSize:14.0];
    label.backgroundColor = [UIColor clearColor];
    [headerView addSubview:label];
    [label release];
    if (channelType==MovieChannelTypeNews) {
        label.text=[NSString stringWithFormat:@"%@: %@",LS(@"发布时间"),[movie News_pub_time]];
    }
    if (channelType==MovieChannelTypeMicroViedo) {
        label.text=[NSString stringWithFormat:@"%@: %@",LS(@"上传者"),[movie News_author]];
    }
    if (channelType==MovieChannelTypeOriginal) {
        label.text=[NSString stringWithFormat:@"%@: %@",LS(@"拍摄时间"),[movie News_arg5]];
    }
    if (channelType==MovieChannelTypeMusic) {
        label.text=[NSString stringWithFormat:@"%@: %@",LS(@"歌手"),[movie News_author]];
    }
    label = [[UILabel alloc]initWithFrame:CGRectMake(95, 60, 200, 20)];
    label.text = [NSString stringWithFormat:@"%@: %@",LS(@"主演"),[movie News_arg2]];
    label.textColor = [UIColor lightGrayColor];
    label.font = [UIFont systemFontOfSize:14.0];
    label.backgroundColor = [UIColor clearColor];
    [headerView addSubview:label];
    [label release];
    
    if (channelType==MovieChannelTypeNews) {
        label.frame=CGRectMake(95, 60, 200, 40);
        label.numberOfLines=0;
    }
    if (channelType==MovieChannelTypeMicroViedo) {
        label.text=[NSString stringWithFormat:@"%@: %@",LS(@"上传时间"),[movie News_arg5]];
    }
    if (channelType==MovieChannelTypeMusic) {
        label.text=[NSString stringWithFormat:@"%@: %@",LS(@"专辑"),[movie News_arg1]];
    }
    if (channelType==MovieChannelTypeOriginal||channelType==MovieChannelTypeNews) {
        label.text=[NSString stringWithFormat:@"%@: %@",LS(@"摘要简介"),[movie News_content]];
    }
    if (channelType!=MovieChannelTypeMusic&&channelType!=MovieChannelTypeNews){
        label = [[UILabel alloc]initWithFrame:CGRectMake(95, 85, 200, 20)];
        label.text = [NSString stringWithFormat:@"%@: %d%@",LS(@"总播放"),[movie News_click_count],LS(@"次")];
        label.textColor = [UIColor lightGrayColor];
        label.font = [UIFont systemFontOfSize:14.0];
        label.backgroundColor = [UIColor clearColor];
        [headerView addSubview:label];
        [label release];
    }
    
    UIButton *bofang = [UIButton buttonWithType:UIButtonTypeCustom];
    bofang.frame = CGRectMake(10, 114, 60, 33);
    bofang.tag = 1001;
    bofang.titleLabel.font = [UIFont systemFontOfSize:12.0];
    [bofang setBackgroundImage:[UIImage imageNamed:@"liangzianniu.png"] forState:UIControlStateNormal];
    [bofang setTitle:LS(@"播放") forState:UIControlStateNormal];
    [bofang addTarget:self action:@selector(btnEvent:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:bofang];
    
    UIButton *xiazai = [UIButton buttonWithType:UIButtonTypeCustom];
    xiazai.frame = CGRectMake(80, 114, 60, 33);
    xiazai.tag = 1002;
    xiazai.titleLabel.font = [UIFont systemFontOfSize:12.0];
    [xiazai setBackgroundImage:[UIImage imageNamed:@"liangzianniu.png"] forState:UIControlStateNormal];
    [xiazai setTitle:LS(@"下载") forState:UIControlStateNormal];
    [xiazai addTarget:self action:@selector(btnEvent:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:xiazai];
    
    UIButton *fenxiang = [UIButton buttonWithType:UIButtonTypeCustom];
    fenxiang.frame = CGRectMake(150, 114, 60, 33);
    fenxiang.tag = 1003;
    fenxiang.titleLabel.font = [UIFont systemFontOfSize:12.0];
    [fenxiang setBackgroundImage:[UIImage imageNamed:@"liangzianniu.png"] forState:UIControlStateNormal];
    [fenxiang setTitle:LS(@"分享") forState:UIControlStateNormal];
    [fenxiang addTarget:self action:@selector(btnEvent:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:fenxiang];
    
    UIButton *shoucang = [UIButton buttonWithType:UIButtonTypeCustom];
    shoucang.frame = CGRectMake(220, 114, 60, 33);
    shoucang.tag = 1004;
    shoucang.titleLabel.font = [UIFont systemFontOfSize:12.0];
    [shoucang setBackgroundImage:[UIImage imageNamed:@"liangzianniu.png"] forState:UIControlStateNormal];
    [shoucang setTitle:LS(@"收藏") forState:UIControlStateNormal];
    [shoucang addTarget:self action:@selector(btnEvent:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:shoucang];
    
    label = [[UILabel alloc]initWithFrame:CGRectMake(10, 148+y, 200, 20)];
    label.text = [NSString stringWithFormat:@"%@:",LS(@"介绍")];
    label.textColor = [UIColor lightGrayColor];
    label.font = [UIFont systemFontOfSize:16.0];
    label.backgroundColor = [UIColor clearColor];
    [headerView addSubview:label];
    [label release];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn addTarget:self action:@selector(showDetailInfo) forControlEvents:UIControlEventTouchDown];
    btn.frame = CGRectMake(10, 148+y, 300, 20);
    [headerView addSubview:btn];
    
    UIImageView *line = [[UIImageView alloc]initWithFrame:CGRectMake(0, 170+y, 320, 2)];
    line.image = [UIImage imageNamed:@"fengexian.png"];
    [headerView addSubview:line];
    [line release];
    
    jiantou = [[UIImageView alloc]initWithFrame:CGRectMake(290, 152+y, 16, 12)];
    jiantou.image = [UIImage imageNamed:@"jiantou.png"];
    [headerView addSubview:jiantou];
    
    NSString *str = [movie News_content];
    detailStr = [[NSString stringWithFormat:@"       %@",str] retain];
    
    infoLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 175+y, 300, 45)];
    infoLabel.text = detailStr;
    infoLabel.numberOfLines = 2;
    infoLabel.textColor = [UIColor lightGrayColor];
    infoLabel.font = [UIFont systemFontOfSize:14.0];
    infoLabel.backgroundColor = [UIColor clearColor];
    [headerView addSubview:infoLabel];
    
    mayLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, headerView.frame.size.height-25, 320, 25)];
    mayLabel.text = [NSString stringWithFormat:@"  %@",LS(@"可能你也感兴趣")];
    mayLabel.textColor = [UIColor whiteColor];
    mayLabel.font = [UIFont systemFontOfSize:14.0];
    mayLabel.backgroundColor = LOW_COLOR;
    [headerView addSubview:mayLabel];
    
    //[self loadPlayAddress];
    [self startDownLoadImage];
    [self loadInterestList];
}

- (void)loadPlayAddress
{
    NSString *action = @"GetDSJAdress";
    NSString *class = @"DataList";
    NSString *rootXML = [NSString stringWithFormat:@"%@Result",action];
    
    CommonWebServices *webServ = [[CommonWebServices alloc] init];
    webServ.tag = 103;
    NSMutableDictionary *parma = [NSMutableDictionary dictionary];
    [parma setValue:[movie News_id] forKey:@"newid"];
    webServ.delegate = self;
    [requestArray addObject:webServ];
    [webServ startRequestWithMode:@"Default" Action:action Parma:parma Class:class RootXML:rootXML resultIsArray:YES];
    [webServ release];
}

- (void)loadInterestList
{
    NSString *interestAction = @"";
    NSString *class = @"";
    
    if (channelType == MovieChannelTypeMovie) {
        interestAction = @"GetInterestedMovideo";
        class = @"Movie";
    }
    else if (channelType == MovieChannelTypeSeries) {
        interestAction = @"GetInterestedDelePlay";
        class = @"DelePlay";
    }
    else if (channelType == MovieChannelTypeMicroViedo) {
        interestAction = @"GetInterestedVideo";
        class = @"Video";
    }
    else if (channelType == MovieChannelTypeMusic) {
        interestAction = @"GetInterestedMusic";
        class = @"Music";
    }
    else if (channelType == MovieChannelTypePic) {
        interestAction = @"GetInterestedPicture";
        class = @"Picture";
    }
    else if (channelType == MovieChannelTypeNews) {
        interestAction = @"GetInterestedMessage";
        class = @"MessageList";
    }
    else if (channelType == MovieChannelTypeOriginal) {
        interestAction = @"GetInterestedOriginal";
        class = @"Original";
    }
    
    class = @"DataList";
    NSString *rootXML = [NSString stringWithFormat:@"%@Result",interestAction];
    
    CommonWebServices *webServ = [[CommonWebServices alloc] init];
    webServ.tag = 102;
    NSMutableDictionary *parma = [NSMutableDictionary dictionary];
    [parma setValue:[movie News_id] forKey:@"newid"];
    webServ.delegate = self;
    [requestArray addObject:webServ];
    [webServ startRequestWithMode:@"Default" Action:interestAction Parma:parma Class:class RootXML:rootXML resultIsArray:YES];
    [webServ release];
}

- (void)startDownLoadImage
{
    if ([movie News_pic]) {
        ImageDownloader* imageDown = [[ImageDownloader alloc] init];
        imageDown.imageURL = [movie News_pic];
        imageDown.delegate = self;
        imageDown.tag = 999;
        [downArray addObject:imageDown];
        [imageDown startDownload:YES];
        [imageDown release];
    }
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
	[downArray makeObjectsPerformSelector:@selector(cancelDownload)];
    [requestArray makeObjectsPerformSelector:@selector(cancelRequest)];
}

- (void)appImageDidLoad:(UIImage *)image withTag:(int)tag{
	if (image) {
        if (tag == 999) {
            UIImageView *imageView = (UIImageView *)[headerView viewWithTag:100];
            imageView.image = image;
			[movie setIconImg:image];
        }
        else {
            id t_movie=[array objectAtIndex:tag];
            UITableViewCell* cell=[table cellForRowAtIndexPath:[NSIndexPath indexPathForRow:tag inSection:0]];
            UIImageView* img=(UIImageView*)[cell.contentView viewWithTag:1000];
            if (image) {
                [t_movie setIconImg:image];
                img.image=image;
            }
        }
    }
}

- (void)showDetailInfo
{
    CGRect frame = headerView.frame;
    
    CGSize size = CGSizeMake(300.0f, 10000.0f);
    CGSize labelSize = [detailStr sizeWithFont:[UIFont systemFontOfSize:14.0] 
                             constrainedToSize:size 
                                 lineBreakMode:UILineBreakModeWordWrap];
    
    CGFloat oldHeight = 36.0;
    
    if (labelSize.height < oldHeight) {
        labelSize.height = oldHeight;
    }
    
    if (showDetail) {
        showDetail = NO;
        [jiantou layer].transform = CATransform3DMakeRotation(0, 0, 0, 1);
        
        CGRect labelFrame = infoLabel.frame;
        labelFrame.size.height = 45;
        infoLabel.frame = labelFrame;
        infoLabel.lineBreakMode = UILineBreakModeTailTruncation;
        infoLabel.numberOfLines = 2;
        
        frame.size.height -= labelSize.height-oldHeight;
        headerView.frame = frame;
    }
    else {
        showDetail = YES;
        [jiantou layer].transform = CATransform3DMakeRotation(M_PI/2, 0, 0, 1);
        
        CGRect labelFrame = infoLabel.frame;
        labelFrame.size.height = labelSize.height + 45 - oldHeight;
        infoLabel.frame = labelFrame;
        infoLabel.lineBreakMode = UILineBreakModeWordWrap;
        infoLabel.numberOfLines = 0;
        
        frame.size.height += labelSize.height-oldHeight;
        headerView.frame = frame;
    }
    
    mayLabel.frame = CGRectMake(0, headerView.frame.size.height-25, 320, 25);
    table.tableHeaderView = headerView;
}

- (void)toPlayVideo
{
    if ([[movie Type] intValue] == MovieChannelTypeSeries) {
        
        NSMutableArray *tmp = [NSMutableArray array];
        for (int i=0; i<[[movie File_path_array] count]; i++) {
            if ([[[movie File_path_array] objectAtIndex:i] isKindOfClass:[NSString class]]) {
                if ([[[movie File_path_array] objectAtIndex:i] length]>5) {
                    [tmp addObject:[[movie File_path_array] objectAtIndex:i]];
                }
            }
        }
        [movie setFile_path_array:tmp];
        
        [movie setOrderNUM:0];
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:PLAYMOVIENOTIFICATION object:movie];
}

- (void)btnEvent:(UIButton *)sender
{
    [movie setDetailType:channelType];
    if (sender.tag == 1001) {
        [self toPlayVideo];
    }
    else if (sender.tag == 1002) {
        if ([movie detailType]==MovieChannelTypeSeries) {
            SeriesSelectingViewController* seriesSelect=[[SeriesSelectingViewController alloc] init];
            seriesSelect.seriesMovie=(Movie*)movie;
            UINavigationController* nav=[[UINavigationController alloc] initWithRootViewController:seriesSelect];

            [self.navigationController presentModalViewController:nav animated:YES];
            
            [seriesSelect release];
            [nav release];
        }else {
            [[NSNotificationCenter defaultCenter] postNotificationName:SENDTODOWNLOAD_NOTIFICATION object:movie];
            
            AppDelegate* app=(AppDelegate*)[[UIApplication sharedApplication] delegate];
            app.mydownloadViewController.title=LS(@"正在下载");
            [self.navigationController pushViewController:app.mydownloadViewController animated:YES];
        }
    }
    else if (sender.tag == 1003) {
        if (isPad || [Utilities detectDevice] == MODEL_IPHONE_SIMULATOR 
			|| [Utilities detectDevice] == MODEL_IPOD_TOUCH ){
			
			UIAlertView *alert = [[UIAlertView alloc]initWithTitle: nil message:LS(@"请使用iPhone发送短信") delegate: self 
												 cancelButtonTitle: LS(@"确定") otherButtonTitles: nil];
			[alert show];
			[alert release];
			return;
			
		}
		
		Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
		
		if ([messageClass canSendText]) {
			[self displayMessageComposerSheet];
		}else {
			[self launchMessageAppOnDevice];
		}
    }
    else if (sender.tag == 1004) {
        NSMutableArray *tmpArray = (NSMutableArray *)[UserDefaults objectForKey:COLLECTION_VIDEOS_STORE_KEY];

        if (!tmpArray) {
            tmpArray = [NSMutableArray array];
        }
        
        BOOL has = NO;
        for (int i=0; i<[tmpArray count]; i++) {
            id hasMovie = [tmpArray objectAtIndex:i];
            
            if ([[hasMovie News_id] isEqualToString:[movie News_id]]) {
                has = YES;
                break;
            }
        }
        CFShow([movie News_content]);
        if (!has) {
            [tmpArray insertObject:movie atIndex:0];
            
            if (tmpArray) {
                [UserDefaults setObject:tmpArray forKey:COLLECTION_VIDEOS_STORE_KEY];
            }
            
            UIAlertView	*alertview = [[UIAlertView alloc] initWithTitle:LS(@"提示")  
                                                                message:LS(@"成功添加到收藏夹")
                                                               delegate:self 								  
                                                      cancelButtonTitle:LS(@"确定")
                                                      otherButtonTitles:nil];		
            [alertview show];		
            [alertview release];
        }
        else {
            UIAlertView	*alertview = [[UIAlertView alloc] initWithTitle:LS(@"提示")  
                                                                message:LS(@"已存在于收藏列表中")
                                                               delegate:self 								  
                                                      cancelButtonTitle:LS(@"确定")
                                                      otherButtonTitles:nil];		
            [alertview show];		
            [alertview release];
        }
        
        
    }
}

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
	NSString *messageResult=nil;
	switch (result) {
		case MessageComposeResultSent:
			messageResult = LS(@"已发送");
			break;
		case MessageComposeResultCancelled:
			messageResult = LS(@"已取消发送");
			break;
		case MessageComposeResultFailed:
			messageResult = LS(@"发送失败");
			break;
		default:
			break;
	}
	
	UIAlertView	*alertview = [[UIAlertView alloc] initWithTitle:LS(@"提示")  
                                                        message:messageResult
                                                       delegate:self 								  
                                              cancelButtonTitle:LS(@"确定")
                                              otherButtonTitles:nil];		
    [alertview show];		
    [alertview release];
    
    [self dismissModalViewControllerAnimated:YES];
}

-(void)launchMessageAppOnDevice{
	
	UIPasteboard*pasteBoard=[UIPasteboard generalPasteboard];
	pasteBoard.string=@"我正在使用56567手机视频客户端；立即点击wap.56567.com/download下载客户端体验下吧！";
	[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms://"]];
}

-(void)displayMessageComposerSheet{
	MFMessageComposeViewController* messageComposeViewController=[[MFMessageComposeViewController alloc] init];
	messageComposeViewController.navigationBar.tintColor = TOOLBAR_TINTCOLOR;
	
	messageComposeViewController.messageComposeDelegate=self;
	messageComposeViewController.body=@"我正在使用56567手机视频客户端；立即点击wap.56567.com/download下载客户端体验下吧！";
	messageComposeViewController.recipients = [NSArray arrayWithObjects:nil];
	
	[self presentModalViewController:messageComposeViewController animated:YES];
	[messageComposeViewController release];		
	
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 115;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [array count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	
	static NSString* ListIdentifier = @"ListIdentifier";
	UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:ListIdentifier];
	
	if (cell == nil)
	{
		cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ListIdentifier];
		//cell.selectionStyle = UITableViewCellSelectionStyleNone;  //去掉选中时的颜色
	}
	
	for (UIView* subView in [cell.contentView subviews])
	{
		[subView removeFromSuperview];
	}
    
	if (indexPath.row < [array count]) {
        
        id movie = [array objectAtIndex:indexPath.row];
		
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 75, 95)];
        image.tag=1000;
        [cell.contentView addSubview:image];
        [image release];
        NSString*imgurl=[movie News_pic];
        //NSLog(@"%@=========\n%@",[movie News_title],imgurl);
        if ((![imgurl isEqual:[NSNull null]])&&imgurl.length>0) {
            //            Cache* cache=[CacheManager queryCacheWithCacheKey:imgurl];
            UIImage*tImg=[movie iconImg];
			if (tImg) {
                //                NSData *data = [NSData dataWithContentsOfFile: cache.cobject];
                //                image.image = [UIImage imageWithData: data];
				image.image=[movie iconImg];
            }else {
                image.image = [UIImage imageNamed:@"dengdai.png"];
				NSDictionary* dic=[NSDictionary dictionaryWithObjectsAndKeys:movie,@"item",indexPath,@"rowNum",nil];
				[self performSelector:@selector(downImg:) withObject:dic afterDelay:0.2];
            }
        }
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(95, 10, 200, 20)];
        label.text = [movie News_title];
        label.textColor = LIGHT_COLOR;
        label.font = [UIFont systemFontOfSize:16.0];
        label.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:label];
        [label release];
		
        label = [[UILabel alloc]initWithFrame:CGRectMake(95, 35, 200, 20)];
        label.text = [NSString stringWithFormat:@"%@: %@",LS(@"导演"),[movie News_arg3]];
        label.textColor = [UIColor lightGrayColor];
        label.font = [UIFont systemFontOfSize:14.0];
        label.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:label];
        [label release];
        if (channelType==MovieChannelTypeNews) {
            label.text=[NSString stringWithFormat:@"%@: %@",LS(@"发布时间"),[movie News_pub_time]];
        }
        if (channelType==MovieChannelTypeMicroViedo) {
            label.text=[NSString stringWithFormat:@"%@: %@",LS(@"上传者"),[movie News_author]];
        }
        if (channelType==MovieChannelTypeOriginal) {
            label.text=[NSString stringWithFormat:@"%@: %@",LS(@"拍摄时间"),[movie News_arg5]];
        }
        if (channelType==MovieChannelTypeMusic) {
            label.text=[NSString stringWithFormat:@"%@: %@",LS(@"歌手"),[movie News_author]];
        }
        label = [[UILabel alloc]initWithFrame:CGRectMake(95, 60, 200, 20)];
        label.text = [NSString stringWithFormat:@"%@: %@",LS(@"主演"),[movie News_arg2]];
        label.textColor = [UIColor lightGrayColor];
        label.font = [UIFont systemFontOfSize:14.0];
        label.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:label];
        [label release];
        
        if (channelType==MovieChannelTypeNews) {
            label.frame=CGRectMake(95, 60, 200, 40);
            label.numberOfLines=0;
            //            label.text=@"乌鲁木齐6月开始进行田字工程施工，届时所有车辆需绕道行驶...";
        }
        if (channelType==MovieChannelTypeMicroViedo) {
            label.text=[NSString stringWithFormat:@"%@: %@",LS(@"上传时间"),[movie News_arg5]];
        }
        if (channelType==MovieChannelTypeMusic) {
            label.text=[NSString stringWithFormat:@"%@: %@",LS(@"专辑"),[movie News_arg1]];
        }
        if (channelType==MovieChannelTypeOriginal||channelType==MovieChannelTypeNews) {
            label.text=[NSString stringWithFormat:@"%@: %@",LS(@"摘要简介"),[movie News_content]];
        }
        if (channelType!=MovieChannelTypeMusic&&channelType!=MovieChannelTypeNews){
            label = [[UILabel alloc]initWithFrame:CGRectMake(95, 85, 200, 20)];
            label.text = [NSString stringWithFormat:@"%@: %d%@",LS(@"总播放"),[movie News_click_count],LS(@"次")];
            label.textColor = [UIColor lightGrayColor];
            label.font = [UIFont systemFontOfSize:14.0];
            label.backgroundColor = [UIColor clearColor];
            [cell.contentView addSubview:label];
            [label release];
        }
        
        UIImageView *line = [[UIImageView alloc]initWithFrame:CGRectMake(0, 114, 320, 2)];
        line.image = [UIImage imageNamed:@"fengexian.png"];
        [cell.contentView addSubview:line];
        [line release];
    }
	
	cell.backgroundColor = [UIColor clearColor];
	
	return cell;
}

- (void)downImg:(NSDictionary*)dic {
	id movie=[dic valueForKey:@"item"];
	int tag=[[dic valueForKey:@"rowNum"] row];
	ImageDownloader* downloader=[[ImageDownloader alloc] init];
	downloader.imageURL=[movie News_pic];
	downloader.delegate=self;
	downloader.tag=tag;
	[downloader startDownload:YES];
	[downArray addObject:downloader];
	[downloader release];
	
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row < [array count]) {
        DetailViewController *detailViewController = [[DetailViewController alloc]init];
        detailViewController.movie = [array objectAtIndex:indexPath.row];
        detailViewController.type = 2;
        detailViewController.channelType = channelType;
        [self.navigationController pushViewController:detailViewController animated:YES];
        [detailViewController release];
    }
}

@end
